package com.treeroot.userservice.user.controller;


import com.treeroot.commonutils.result.R;
import com.treeroot.commonutils.utils.JwtUtils;

import com.treeroot.commonutils.utils.MD5;
import com.treeroot.userservice.user.service.AclUserService;
import com.treeroot.userservice.user.entity.AclUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author TreeRoot
 * @since 2022-06-09
 */
@RestController
@Api(description = "登录管理")
@RequestMapping("/user/admin")
@CrossOrigin
public class AclUserController {

    @Autowired
    AclUserService userService;
    
    
    /**
     * login
     *
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody AclUser aclUser) {
      String token   =   userService.login(aclUser);
        return R.ok().data("token", token);
    }

    /**
     * info
     *
     * @return
     */
    @GetMapping("info")
    public R info(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        AclUser user = userService.getById(memberId);
        return R.ok()
//        用户
                .data("name", "许根银,您好")
                .data("roles", "超级管理员")
//                头像
                .data("avatar", "https://guli-parent-tree-root.oss-cn-hangzhou.aliyuncs.com/2022%3A4%3A16%3A%E5%A4%B4%E5%83%8F.png");
    }

    @PostMapping("/logout")
    public R logout() {
        return R.ok().success(true);

    }


/**
 * echarts
 */
@GetMapping("count")
public  R count (){
    int count = userService.count(null);
    return R.ok().data("adminCount",count);
}


    /**
     * @param current 当前页
     * @param limit   最大页
     * @param admin   实体数据
     */
//    条件查询带分页的方法
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pageAdminCondition/{current}/{limit}")
    public R pageadminCondition(@PathVariable long current, @PathVariable long limit,
                                @RequestBody(required = false) AclUser admin) {

        Map map = userService.getQueryAdminList(current, limit, admin);

        return R.ok().data(map);

    }

    /**
     * 根据ID查询用户
     *
     * @param id
     *
     */

    @ApiOperation(value = "根据ID获取用户信息")
    @GetMapping("getUser/{id}")
    public R getUserById(
            @PathVariable String id) {
        AclUser user = userService.getById(id);
        return R.ok().data("user", user);
    }



    @ApiOperation(" 添加用户信息")
    @PostMapping("addUser")
    public R addUser(@RequestBody AclUser user) {
        //        密码不加密问题
        user.setPassword(MD5.encrypt(user.getPassword()));
        userService.save(user);
        return R.ok();
    }


    /**
     * @param id  用户 id
     */
    @ApiOperation(value = "根据ID删除用户")
    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable String id) {
        userService.removeById(id);

        return R.ok();
    }

    /**
     * 修改用户信息
     *
     * @param user 用户信息 实体类
     */
    @ApiOperation(value = "修改用户信息")
    @PutMapping("updateUser")
    public R updateUser(@RequestBody AclUser user) {

        //        密码不加密问题
        user.setPassword(MD5.encrypt(user.getPassword()));
        userService.updateById(user);
        return R.ok();
    }


}

